package cn.jinjimi.app.main.widget.ToolBar;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;

/**
 * Created by Roy
 * Date: 16/7/25
 * @author zhenhua
 */
public class CommonToolbar extends android.support.v7.widget.Toolbar {

    private Button mBtnLeft, mBtnRight;
    private TextView mTitleView;

    public CommonToolbar(Context context) {
        this(context, null, 0);
    }

    public CommonToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.common_toolbar_layout, this);
        initView();
    }

    private void initView() {
        mBtnLeft = (Button) findViewById(R.id.btn_left);
        mBtnRight = (Button) findViewById(R.id.btn_right);
        mTitleView = (TextView) findViewById(R.id.tv_title_bar_title);
    }

    public final void setTitle(String title) {
        mTitleView.setText(title);
    }

    @Override
    public final void setTitle(CharSequence title) {
        mTitleView.setText(title);
    }

    @Override
    public final void setTitle(int strRes) {
        mTitleView.setText(strRes);
    }

    /**
     * 普通的标题栏 只有标题和返回按钮
     *
     * @param title
     */
    public final void baseTitle(CharSequence title) {
        ensureInit();
        mTitleView.setText(title);
        showLeftButton(true);
    }

    /**
     * 设置标题颜色
     *
     * @param textColor
     */
    public void setTitleColor(int textColor) {
        ensureInit();
        mTitleView.setTextColor(textColor);
    }

    /**
     * 设置标题右边icon
     *
     * @param resId
     */
    public void setTitleDrawable(int resId) {
        ensureInit();
        if (resId > 0) {
            Drawable drawable = ContextCompat.getDrawable(getContext(), resId);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            mTitleView.setCompoundDrawables(null, null, drawable, null);
        } else {
            mTitleView.setCompoundDrawables(null, null, null, null);
        }
    }

    /**
     * 设置左边按钮文字
     *
     * @param text
     */
    public void setLeftButton(String text) {
        setLeftButton(0, text);
    }

    /**
     * 设置左边图标
     *
     * @param resId
     */
    public void setLeftButton(int resId) {
        setLeftButton(resId, "");
    }

    /**
     * 设置左边文字与图标
     *
     * @param resId
     * @param text
     */
    public void setLeftButton(int resId, String text) {
        ensureInit();
        if (resId > 0) {
            Drawable drawable = ContextCompat.getDrawable(getContext(), resId);
            /// 这一步必须要做,否则不会显示.
            mBtnLeft.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
            showLeftButton(true);
        } else {
            mBtnLeft.setCompoundDrawables(null, null, null, null);
        }
        if (StringUtils.isNotEmpty(text)) {
            mBtnLeft.setText(text);
            showLeftButton(true);
        }
    }

    /**
     * 设置右边文字
     *
     * @param text
     */
    public void setRightButton(String text) {
        setRightButton(0, text);
    }

    /**
     * 设置右边图标
     *
     * @param resId
     */
    public void setRightButton(int resId) {
        setRightButton(resId, "");
    }

    /**
     * 设置右边文字与图标
     *
     * @param resId
     * @param text
     */
    public void setRightButton(int resId, String text) {
        ensureInit();
        if (resId > 0) {
            Drawable drawable = ContextCompat.getDrawable(getContext(), resId);
            mBtnRight.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
            showRightButton(true);
        } else {
            mBtnRight.setCompoundDrawables(null, null, null, null);
        }
        mBtnRight.setText(text);
        showRightButton(true);
    }

    public void setRightButtonMargin(int left, int top, int right, int bottom){
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)mBtnRight.getLayoutParams();
        lp.bottomMargin = bottom;
        lp.topMargin = top;
        lp.leftMargin = left;
        lp.rightMargin = right;
        mBtnRight.setLayoutParams(lp);
    }

    /**
     * 设置右边文字颜色
     *
     * @param csl
     */
    public void setRightButtonTextColor(ColorStateList csl) {
        ensureInit();
        mBtnRight.setTextColor(csl);
    }

    public void showLeftButton(boolean show) {
        ensureInit();
        mBtnLeft.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void showRightButton(boolean show) {
        ensureInit();
        mBtnRight.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public void setRightBtnEnable(boolean enable) {
        ensureInit();
        if (mBtnRight.isShown()) {
            mBtnRight.setEnabled(enable);
        }
    }

    private void ensureInit() {
        if (mBtnLeft == null) {
            mBtnLeft = (Button) findViewById(R.id.btn_left);
        }
        if (mBtnRight == null) {
            mBtnRight = (Button) findViewById(R.id.btn_right);
        }
        if (mTitleView == null) {
            mTitleView = (TextView) findViewById(R.id.tv_title_bar_title);
        }
    }

    public Button getRightButton() {
        ensureInit();
        return mBtnRight;
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        ensureInit();
        mBtnLeft.setOnClickListener(l);
        mBtnRight.setOnClickListener(l);
        mTitleView.setOnClickListener(l);
        super.setOnClickListener(l);
    }
}

